package com.jdy.haoduoaiteacher.activity;

import android.app.AlertDialog;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebSettings.PluginState;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.jdy.haoduoaiteacher.util.Tools;
import com.ycsj.goldmedalnewconcept.R;

//import com.jdy.haoduoaiteacher.R;

/**
 * Get LeXiaoBao forum of to this page!
 * 
 * @author zp @ 2015-6-25 上午9:57:41
 */
public class WebViewContainerActivity extends BaseActivity {

  private static final String TAG = WebViewContainerActivity.class
      .getSimpleName();

  private WebView webview;

  private boolean mUseWebTitle = true;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.le_xiao_bao_forum_layout);

    if (Tools.isNotEmpty(getIntent().getStringExtra("speak"))) {
      mUseWebTitle = false;
      setTitle("小小班育儿搜索");
    }

    // setTitle("小小班社区");
    this.webview = (WebView) findViewById(R.id.webView1);

    showloading("");
    WebSettings settings = webview.getSettings();
    settings.setBuiltInZoomControls(true);
    settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
    settings.setUseWideViewPort(true);
    settings.setLoadWithOverviewMode(true);
    // settings.setSavePassword(true);
    settings.setSaveFormData(true);
    settings.setJavaScriptEnabled(true);
    settings.setDomStorageEnabled(true);
    settings.setAllowFileAccess(true);
    settings.setJavaScriptCanOpenWindowsAutomatically(true);
    settings.setSupportZoom(true);// 是否可以缩放，默认true

    // settings.setSupportMultipleWindows(true);
    settings.setPluginState(PluginState.ON);
    settings.setAppCacheEnabled(true);// 是否使用缓存

    if (Tools.isNetworkAvailable2(this)) {
      settings.setCacheMode(WebSettings.LOAD_DEFAULT);
    } else {
      settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
    }

    webview.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);

    final AlertDialog alertDialog = new AlertDialog.Builder(this).create();

    webview.setWebViewClient(new WebViewClient() {

      // @Override
      // public void onLoadResource(WebView view, String url)
      // {
      // if (url.equals("http://bbs.xiaoxiaoban.net"))
      // {
      // //do your own thing here
      // Log.e(TAG, "somthing happend here!");
      // }
      // else
      // {
      // super.onLoadResource(view, url);
      // }
      // }
      //
      @Override
      public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
      }

      @Override
      public void onPageStarted(WebView view, String url, Bitmap favicon) {
        notifyLoadingStart();
        super.onPageStarted(view, url, favicon);
      }

      @Override
      public void onPageFinished(WebView view, String url) {
        notifyLoadingStop();

        dismissLoading();

        if (mUseWebTitle)
          setTitle(webview.getTitle());
      }

      @Override
      public void onReceivedError(WebView view, int errorCode,
          String description, String failingUrl) {
        Log.e(TAG, "出现了错误: " + description);
        // toastShow( "噢!出现了点儿问题!" );
        // alertDialog.setTitle("错误");
        // alertDialog.setMessage(description);
        // alertDialog.setButton("确定", new DialogInterface.OnClickListener() {
        // @Override
        // public void onClick(DialogInterface dialog, int which) {
        // return;
        // }
        // });
        // alertDialog.show();
      }
    });

    String url = null;
    if (getIntent() != null) {
      url = getIntent().getStringExtra("h5url");
    }

    if (Tools.isNotEmpty(url)) {
      webview.loadUrl(url);
    } else {
      webview.loadUrl("http://bbs.xiaoxiaoban.net");
    }
  }

  @Override
  public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {
      webview.goBack();
      return true;
    }
    return super.onKeyDown(keyCode, event);
  }
}
